<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
	
	private $publickey = "6LeOtd8SAAAAANPWbZGvIHWiZyZVH--NjX1vVCx3";
	private $privatekey = "6LeOtd8SAAAAAKRmyJw_UC99l5q6Q6KRvGEteLhQ";
	
	function __construct()
	{
		parent::__construct();
		$this->config->set_item('csrf_protection', true);
		$this->load->model(array('Accountmodel'));
		
	}
	
	public function index($value='')
	{
		
		//echo isset($_GET['uid']);
		$data['user'] = "";
		//if(count($this->session->userdata('userlog')) < 1){
			
			if(isset($_GET['uid']) || $value != ""){
				$data['user'] = $this->__get_user(isset($_GET['uid']) ? $_GET['uid'] : $value);
				if(count($data['user']) < 1){
					show_error("User not found");
				}
			}elseif(!isset($_GET['uid']) || $value == ""){
				if($this->session->userdata('userlog')){
					$data['user'] = $this->session->userdata('userlog');
				}else{
					//show_error("User not found");
					redirect("login");
				}
				
			}
			
		//}else{
		//	$data['user'] = $this->session->userdata('userlog');
		//}

		$this->template->write('title', 'Profile');
		$this->template->write_view('header', 'global/header',$data);
		$this->template->write_view('content', 'user/profile');
		$this->template->render();
	}
	
	function __get_user($value='')
	{
		return $this->Accountmodel->get_user_info($value);
	}
	
	public function logout()
	{
		$this->session->unset_userdata('userlog');
		redirect('/');
	}
	
	public function login()
	{
		//$userlog = $this->session->userdata('userlog');
		//var_dump($userlog[0]->acct);
		//$_SERVER["REMOTE_ADDR"];
		if($this->input->post()):
			$info = $this->Accountmodel->login();
			
			if(count($info) > 0){
				$this->Accountmodel->login_update($info[0]->acct);
				$this->session->set_userdata(array('userlog'=>$info));
				redirect("/user");
			}else{
				$this->session->set_flashdata('msg_login','Username and Password did not match!');
				redirect("/login");
			}
		endif;
		
		$this->template->write('title', 'Login');
		$this->template->write_view('header', 'global/header');
		$this->template->write_view('content', 'user/login');
		$this->template->render();
	}
	
	public function register()
	{
		$this->load->helper(array('my_captcha'));
		$this->load->library('form_validation');
		
		// Get a key from https://www.google.com/recaptcha/admin/create
		
		
		$data['publickey'] = $this->publickey;
		$data['privatekey'] = $this->privatekey;
		
		
		# the response from reCAPTCHA
		$resp = null;
		# the error code from reCAPTCHA, if any
		$error = null;
		
		$data['resp'] = $resp;
		$data['error'] = $error;
		
		$this->template->write('title', 'Register');
		$this->template->write_view('header', 'global/header');
		
		$this->form_validation->set_error_delimiters('<div class=\"error\">', '</div>');
		$this->form_validation->set_rules('username', 'Username', 'required|callback_check_username');
		$this->form_validation->set_rules('password', 'Password', 'required|matches[cpassword]|min_length[5]');
		$this->form_validation->set_rules('cpassword', 'Password Confirmation', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|matches[cemail]|callback_check_email');
		$this->form_validation->set_rules('cemail', 'Email Confirmation', 'required');
		$this->form_validation->set_rules('recaptcha_response_field', 'Security Code', 'required|callback_check_captcha');
		$this->form_validation->set_rules('agree', 'Terms & Conditions', 'callback_check_agree');
		
		if ($this->form_validation->run() == TRUE)
		{
			$this->load->library('email');
			$this->email->from('admin@no-reply.com', 'Onyx Admin');
			$this->email->to($this->input->post('email')); 
			

			$data['message'] = 	"Hi ".$this->input->post('username').
										",<br/><br/>WELCOME! Your are successfully registered!<br/><br/>Username: ".
										$this->input->post('username')."<br/>Password: ".$this->input->post('password').
										"<br/><br/>Thank you";
			
			$msg = $this->load->view('email_template',$data,true);
			
			$this->email->subject('Onyxfang: Registration');
			$this->email->message($msg);	

			$this->email->send();
			
			//email admin
			$data['message'] = 	"Hi Admin,<br/><br/>New user registered<br/>Username: ".$this->input->post('username')."<br/><br/>Thank you";
			
			$msg2 = $this->load->view('email_template',$data,true);
			
			$this->email->to('ronneltumbaga@gmail.com'); 
			$this->email->subject('Onyxfang: New user');
			$this->email->message($msg2);	

			$this->email->send();
			
			$this->Accountmodel->insert_account();
			$this->session->set_flashdata('msg_reg','You have successfuly registered.');
			redirect('register');
		}
		
		
		$this->template->write_view('content', 'user/register',$data);
		$this->template->render();
	}
	
	function check_email($email){
		$this->db->select('acct');
		$query = $this->db->get_where('accounts',array('email'=>$email));
		$result = $query->result();
		//echo count($result); exit;
		if(count($result)){
			$this->form_validation->set_message('check_email', 'Email exist!');
			return false;
		}else{
			return true;
		}
	}
	
	function check_username($username){
		$this->db->select('acct');
		$query = $this->db->get_where('accounts',array('login'=>$username));
		$result = $query->result();
		//echo count($result); exit;
		if(count($result)){
			$this->form_validation->set_message('check_username', 'Username exist!');
			return false;
		}else{
			return true;
		}
	}
	
	function check_agree(){
		if($this->input->post('agree')){
			return true;
		}else{
			$this->form_validation->set_message('check_agree', 'Accept terms & conditions!');
			return false;
		}
	}
	
	function check_captcha(){
		$resp = recaptcha_check_answer ($this->privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $this->input->post('recaptcha_challenge_field'),//$_POST["recaptcha_challenge_field"],
                                        $this->input->post('recaptcha_response_field'));//$_POST["recaptcha_response_field"]);
			if ($resp->is_valid) {
 				return true;
			} else {
				# set the error code so that we can display it
				$this->form_validation->set_message('check_captcha', 'Security code invalid!');
				//return $data['error'] = $resp->error;
				return false;
			}
	}
}

/* End of file user.php */
/* Location: ./onyxapplication/controllers/user.php */